package com.combetter.changxing.ui.fragment;

import android.content.Intent;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.combetter.changxing.MyApp;
import com.combetter.changxing.R;
import com.combetter.changxing.databinding.FragmentMineUserInfoBinding;
import com.combetter.changxing.model.EventMessage;
import com.combetter.changxing.model.parameter.RegisterParameter;
import com.combetter.changxing.ui.base.BaseFragment;
import com.combetter.changxing.ui.dialog.BelowSelectDialog;
import com.combetter.changxing.ui.dialog.CityListDialog;
import com.combetter.changxing.util.DataUtils;
import com.combetter.changxing.viewmodel.MineUserInfoViewModel;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.File;
import java.util.List;

import static com.combetter.changxing.constanst.Constants.REUEST_CODDE;

/**
 * 修改个人资料Fragment
 * <p>
 * Created by 深圳唐龙飞讯科技有限公司 on 2018/6/13.
 */
public class MineUserInfoFragment extends BaseFragment<FragmentMineUserInfoBinding, MineUserInfoViewModel> {
    private RegisterParameter parameter;

    @Override
    public int setLayout() {
        return R.layout.fragment_mine_user_info;
    }


    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void init() {
        mBinding.tvPhone.setText(DataUtils.hideMobilePhone4(MyApp.getInstance().getUserInfo().getPhone()));
    }

    @Override
    public Class createViewModel() {
        return MineUserInfoViewModel.class;
    }

    @Override
    public void loadData() {
        if (MyApp.getInstance().getUserInfo() != null) {
            mBinding.setUser(MyApp.getInstance().getUserInfo());
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (MyApp.getInstance().getUserInfo() != null) {
            mBinding.setUser(MyApp.getInstance().getUserInfo());
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == getActivity().RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    // 图片选择结果回调
                    List<LocalMedia> selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    if (selectList.size() > 0) {
                        if (selectList.get(0).isCompressed()) {
                            File file = new File(selectList.get(0).getCompressPath());
                            mViewModel.updateHead(getContext(), file);
                        }
                    }
                    break;
            }
        }
        if (requestCode == REUEST_CODDE) {
            String stringExtra = data.getStringExtra(BelowSelectDialog.REQUESE);
            int titleId = data.getIntExtra(BelowSelectDialog.REQUESEID, 0);
            if (titleId == getViewModel().sexId) {
                int saveSex = 1;
                if (stringExtra.equals("男")) {
                    saveSex = 1;
                } else if (stringExtra.equals("女")) {
                    saveSex = 2;
                }
                parameter = new RegisterParameter();
                parameter.setSex(saveSex + "");
                mViewModel.alterInfo(getContext(), parameter);

            } else if (titleId == getViewModel().ageId) {
                parameter = new RegisterParameter();
                parameter.setAge(stringExtra);
                mViewModel.alterInfo(getContext(), parameter);
            }
            int cityId = data.getIntExtra("cityId", 0);
            if (cityId == CityListDialog.cityId) {
                String cityName = data.getStringExtra("cityName");
                parameter = new RegisterParameter();
                parameter.setCity(cityName);
                mViewModel.alterInfo(getContext(), parameter);
            }
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void getEventData(EventMessage message) {
        switch (message.getType()) {
            case "update":
                if (MyApp.getInstance().getUserInfo() != null) {
                    mBinding.setUser(MyApp.getInstance().getUserInfo());
                }
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }
}
